package com.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.model.User;
import com.services.UserService;

@Controller
@RequestMapping
public class UserController {

	@Autowired
	private UserService userService;
    
	@RequestMapping("/reg")
	public String saveUser(User user, HttpServletRequest req) {
		user.setCreatedate(new Date());
		userService.save(user);
		System.out.println("hahah");
		return "success";
	}

	@RequestMapping("/query")
	public String queryUser(Integer id, HttpServletRequest req) {

		List<User> list = userService.query(id);
		req.setAttribute("users", list);
		return "success";
	}

	@RequestMapping("/delete")
	public String deleteUser(int id, HttpServletRequest req) {

		userService.deleteUser(id);
		return "success";
	}
	

	@RequestMapping("/update")
	public String update(int id, HttpServletRequest req) {

		userService.updateUser(id);
		return "success";
	}
	
	@RequestMapping("/login")
	public String userLogin(String username,String password, HttpServletRequest req) {

		List<User> list = userService.login(username,password);
		System.out.println(list.size());
		if(list.size()==1){
		req.setAttribute("users", list);
		return "success";
		}else{
			return "../../login";
		}
	}

}